package com.mutable.app.service.impl;

import com.mutable.app.entity.User;
import com.mutable.app.mapper1.UserMapper;
import com.mutable.app.mapper2.User2Mapper;
import com.mutable.app.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
public class UserServiceImpl implements UserService {
    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private User2Mapper user2Mapper;

    public boolean saveUser(User user) {
        boolean flag1 = userMapper.saveUser(user);
        logger.info("save user flag1 is {}", flag1);

        User flag2 = user2Mapper.save(user);
        logger.info("save user flag2 is {}", flag2);
        return true;
    }

    @Override
    @Transactional
    public boolean updateUser(User user) {
        User user1 = userMapper.findById(user.getId());
        if(user1 == null) {
            logger.error("user is null error {}", user.getId());
        }

        user1.setName(user.getName());
        userMapper.updateUser(user);
        logger.info("source1 update user success!");

        Optional<User> user2 = user2Mapper.findById(user.getId());
        if(!user2.isPresent()) {
            logger.error("source2 is null errr {} " , user.getId());
        }

        user2.get().setName(user.getName());
        user2Mapper.save(user2.get());
        logger.info("sorce2 update user success!");

        int i = 1/0;


        return false;
    }

}
